package cn.assumejson.pro_netty.netty.client.asyn;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 多线程Client
 * 主线程模拟发送请求，开启额外线程模拟获取响应结果
 *
 * @author by gang.Li 2022/3/12 10:46
 */
public class FutureMain {
    private static final BlockingQueue workQueue = new LinkedBlockingQueue(1024);

    public static void main(String[] args) {
        // request list
        List<RequestFuture> reqs = new ArrayList<>();
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(20, 100, 5, TimeUnit.SECONDS, workQueue);
        for (int i = 1; i < 100; i++) {
            RequestFuture req = new RequestFuture();
            req.setRequest("HELLO WORLD");
            // 缓存请求
            RequestFuture.addFuture(req);
            reqs.add(req);
            sendMsg(req);
            poolExecutor.execute(new SubThread(req));
        }
    }

    private static void sendMsg(RequestFuture req) {
        System.out.println("客户端发送求情数据，请求id为====" + req.getId());
    }
}
